//
//  GYLoginViewModel.m
//  GYDemo
//
//  Created by Mrzhang on 2018/3/22.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "GYLoginViewModel.h"
#import "Head.h"
#import "GYMainTab.h"
@implementation GYLoginStatusModel
@end
@implementation GYLoginInfoModel
+(NSString *)getTableName
{
    return @"GYLoginInfoModel";
}
@end

@interface GYLoginViewModel ()

@end
@implementation GYLoginViewModel
#pragma mark - getter and setter
//-(GYLoginInfoModel *)loginInfoModel
//{
//    NSString *sql = [NSString stringWithFormat:@"select * from GYLoginInfoModel where loginmobile = %@",self.username];
//    NSLog(@"%@",NSHomeDirectory());
//    NSArray *arr = [GYLoginInfoModel searchWithSQL:sql];
//    GYLoginInfoModel *model;
//    if (arr.count > 0) {
//        model = arr[0];
//    }
//    return model;
//}
-(GYLoginStatusModel *)loginStatusModel
{
    if (!_loginStatusModel) {
        _loginStatusModel=[[GYLoginStatusModel alloc] init];
    }
    return _loginStatusModel;
}
-(NSString *)username
{
    if (!_username) {
        _username = [[NSUserDefaults standardUserDefaults]objectForKey:kLoginUsername];
    }
    return _username;
}
-(NSInteger)roleType
{
    if (!_roleType) {
        _roleType= 1;
    }
    return _roleType;
}
#pragma mark - method
+(instancetype)shareInterface
{
    static GYLoginViewModel* model;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!model) {
            model=[GYLoginViewModel new];
        }
    });

    return model;
}
-(instancetype)init
{
    if (self = [super init]) {
        [[InterfaceManager shareInterface].specialInterfaces addObject:@"login"];
        [[InterfaceManager shareInterface].specialInterfaces addObject:@"storageLogin"];
        [[InterfaceManager shareInterface].specialInterfaces addObject:@"logisticsLogin"];
    }
    return self;
}
-(void)requsetLogin
{
    NSMutableDictionary* paraDic=[[NSMutableDictionary alloc] init];
    paraDic[@"username"]=self.username;
    paraDic[@"psw"]=self.password;
    [FTIndicator showProgressWithMessage:@"正在登录"];
    @WeakSelf;
    
    [[InterfaceManager shareInterface] requetInterface:@"user_login" withParameter:paraDic handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {

        if ([[info objectForKey:@"code"] integerValue]==1) {
            [FTIndicator showErrorWithMessage:info[@"msg"]];
        }else{
            [FTIndicator showSuccessWithMessage:@"登录成功"];
            NSDictionary *data = info;
            GYLoginInfoModel* model=[[GYLoginInfoModel alloc] init];
            model = [GYLoginInfoModel yy_modelWithJSON:data];
            [model saveToDB];
            [[NSUserDefaults standardUserDefaults]setObject:model.mobile forKey:kLoginUsername];
            [[NSUserDefaults standardUserDefaults]setObject:selfp.password forKey:kLoginPsw];
            [[NSUserDefaults standardUserDefaults]setObject:[NSNumber numberWithInteger:selfp.roleType] forKey:kUserIdentity];
            [[GYLoginViewModel shareInterface] setLoginstatus:YES];
            [[GYLoginViewModel shareInterface] setLoginInfoModel:model];
        }
    }];
}
-(void)requestDefaultLogin
{
    NSMutableDictionary* paraDic=[[NSMutableDictionary alloc] init];
    paraDic[@"uname"]=[[NSUserDefaults standardUserDefaults]objectForKey:kLoginUsername];
    paraDic[@"psw"]=[[NSUserDefaults standardUserDefaults]objectForKey:kLoginPsw];

     NSInteger logType= [[[NSUserDefaults standardUserDefaults] objectForKey:kUserIdentity] integerValue];
    
    @WeakSelf;
    [[InterfaceManager shareInterface] requetInterface:logType==1?@"login":logType==2?@"storageLogin":@"logisticsLogin" withParameter:paraDic handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        [FTIndicator dismissProgress];
        if ([[info objectForKey:@"status"] integerValue]==0)
        {
            NSDictionary *data = info[@"data"];
           
            GYLoginInfoModel* model=[[GYLoginInfoModel alloc] init];
            if (logType==1)
            {
                model = [GYLoginInfoModel yy_modelWithJSON:data];
            }
            if (logType==2)
            {
                model.loginmobile=[data objectForKey:@"mobile"];
                model.access_token=[data objectForKey:@"access_token"];
            }
            if (logType==3)
            {
                model.loginmobile=[data objectForKey:@"mobile"];
                model.access_token=[data objectForKey:@"access_token"];
            }
            // GYLoginInfoModel *model = [GYLoginInfoModel yy_modelWithJSON:data];
            [[InterfaceManager shareInterface].httpManager.requestSerializer setValue:model.access_token forHTTPHeaderField:@"Authorization"];
            [model saveToDB];
            [[NSUserDefaults standardUserDefaults]setObject:model.loginmobile forKey:kLoginUsername];
            [[NSUserDefaults standardUserDefaults]setObject:[NSNumber numberWithInteger:logType] forKey:kUserIdentity];
            [[GYLoginViewModel shareInterface] setLoginstatus:YES];
            [selfp setLoginInfoModel:model];
            
            if (selfp.roleType==1)
            {
                [[GYUserViewModel shareInterface]requestUserInfo];
            }
            if (selfp.roleType==2)
            {
                GYStorageUserModel* storageUsermodel=[GYStorageUserModel yy_modelWithJSON:data];
                [[GYStorageUserViewModel shareInterface] setUserModel:storageUsermodel];
                [[GYStorageUserViewModel shareInterface] requestUserShopBaseInfo];
            }
            if (selfp.roleType==3)
            {
                GYStorageUserModel* storageUsermodel=[GYStorageUserModel yy_modelWithJSON:data];
                [[GYStorageUserViewModel shareInterface] setUserModel:storageUsermodel];
                [[GYStorageUserViewModel shareInterface] requestUserShopBaseInfo];
            }
            
//            [UIApplication sharedApplication].delegate.window.rootViewController=[[GYMainTab alloc] init];
            [UIApplication sharedApplication].delegate.window.rootViewController = [GCPresenter shareInterface].mainVC;
        }else
        {

            
        }
        
        
    }];
}
-(void)requsetLogout
{
    [self.loginInfoModel deleteToDB];
    [self setLoginInfoModel:nil];
    [[GYStorageUserViewModel shareInterface] setStorageUserInfoModel:nil];
    [[GYUserViewModel shareInterface] logout];
    [[GYLoginViewModel shareInterface] setLoginstatus:NO];
    [[NSUserDefaults standardUserDefaults]setObject:@"" forKey:kLoginUsername];
    [[NSUserDefaults standardUserDefaults]setObject:[NSNumber numberWithInteger:1] forKey:kUserIdentity];
    
    [[GCPresenter shareInterface] loadChooseVC];

}
@end
